package com.stellar.circle.basic.dao;

import com.stellar.circle.basic.entity.SComment;
import com.stellar.circle.controller.admin.sComment.vo.CommentStatistics;
import com.stellar.circle.controller.admin.sComment.vo.DailyCommentStatisticsVO;
import com.stellar.circle.controller.admin.sComment.vo.SCommentWithUserInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * 评论表(SComment)数据访问层
 *
 * @author Jqchan
 * @since 2025-05-19 14:52:25
 */
public interface SCommentDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键ID
     */
    SComment queryById(@Param("id") Long id);

    /**
     * 条件查询单条结果
     *
     * @param condition 查询条件
     */
    SComment queryByCondition(@Param("condition") SComment condition);

    /**
     * 条件查询所有结果
     *
     * @param condition 查询条件
     */
    List<SComment> queryAllByCondition(@Param("condition") SComment condition);

    /**
     * 分页查询
     *
     * @param condition 查询条件
     * @param page      页码
     * @param pageSize  每页数量
     */
    List<SComment> page(@Param("condition") SComment condition,
                        @Param("page") int page,
                        @Param("pageSize") int pageSize);

    /**
     * 条件统计
     *
     * @param condition 统计条件
     */
    Long count(@Param("condition") SComment condition);

    /**
     * 新增数据
     *
     * @param entity 实例对象
     */
    int insert(@Param("entity") SComment entity);

    /**
     * 批量新增
     *
     * @param entities 实例列表
     */
    int insertBatch(@Param("entities") List<SComment> entities);

    /**
     * 更新数据
     *
     * @param entity 实例对象
     */
    int update(@Param("entity") SComment entity);

    /**
     * 通过主键删除
     *
     * @param id 主键ID
     */
    int deleteById(@Param("id") Long id);


    /**
     * 根据父评论id批量查询
     *
     * @param parentCommentIds
     * @return
     */
    List<SComment> batchQueryByParentIds(@Param("parentCommentIds") Set<Long> parentCommentIds);

    /**
     * 获取评论统计数据
     *
     * @param beginTime     上一个周期的开始时间
     * @param endTime       上一个周期的结束时间
     * @param sourceType    来源类型
     * @param isAnonymous   是否匿名
     * @param commentStatus 评论状态
     * @return
     */
    CommentStatistics getCommentStatistics(
            @Param("beginTime") LocalDateTime beginTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("sourceType") String sourceType,
            @Param("isAnonymous") Integer isAnonymous,
            @Param("commentStatus") String commentStatus
    );


    /**
     * 获取过去一周内每天的评论数
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    @Select({
            "<script>",
            "SELECT ",
            "   DATE_FORMAT(create_time, '%m-%d') AS date, ",
            "   COUNT(*) AS count ",
            "FROM s_comment ",
            "WHERE is_deleted = 0 ",
            "   AND create_time BETWEEN #{beginTime} AND #{endTime} ",
            "GROUP BY DATE_FORMAT(create_time, '%m-%d') ",
            "ORDER BY create_time ASC",
            "</script>"
    })
    List<DailyCommentStatisticsVO> getDailyCommentCounts(@Param("beginTime") LocalDateTime beginTime,
                                                         @Param("endTime") LocalDateTime endTime);

    /**
     * 更新评论回复数
     *
     * @param parentId
     * @param delta
     */
    @Update("UPDATE s_comment SET reply_count = reply_count + #{delta} WHERE id = #{parentId}")
    void updateReplyCount(@Param("parentId") Long parentId,
                          @Param("delta") int delta);

    // 获取所有未被删除的评论（带用户信息）
    @Select({
            "<script>",
            "SELECT c.*, u.nick_name, u.avatar",
            "FROM s_comment c",
            "JOIN auth_user u ON c.user_id = u.id",
            "WHERE c.source_type = #{sourceType}",
            "AND c.source_id = #{sourceId}",
            "AND c.is_deleted = 0",
            "AND c.is_hidden = 0",
            "AND c.comment_status = 'published'",
            "</script>"
    })
    List<SCommentWithUserInfo> selectAllCommentsWithUserInfo(@Param("sourceType") String sourceType,
                                                             @Param("sourceId") Long sourceId);

    // 获取所有一级评论
    @Select({
            "<script>",
            "SELECT c.*, u.nick_name, u.avatar",
            "FROM s_comment c",
            "JOIN auth_user u ON c.user_id = u.id",
            "WHERE c.source_type = #{sourceType}",
            "AND c.source_id = #{sourceId}",
            "AND c.parent_id IS NULL",
            "AND c.is_deleted = 0",
            "AND c.is_hidden = 0",
            "AND c.comment_status = 'published'",
            "</script>"
    })
    List<SCommentWithUserInfo> selectRootComments(@Param("sourceType") String sourceType,
                                                  @Param("sourceId") Long sourceId);
}

